let cmd = {
	cmd = 0x4100,
	fsn = 123,
	data = ""
};

let ble = {
	pendingCmds = {},
}

ble.decodeShipFrame = function(bytes) {
	// bytes -> frame
	return frame;
}

ble.encodeShipFrame = function(frame) {
	// frame -> bytes
	return bytes;
}

// 传输层发送
ble.send = function(bytes) {
	let count = bytes.length / MAX_BLE_PAYLOAD + 1;
	for(let i=0; i<count; i++) {
		let len = bytes.length - i*MAX_BLE_PAYLOAD;
		len = min(len, MAX_BLE_PAYLOAD);
		await wxble.send(bytes.get(i*MAX_BLE_PAYLOAD, len));
	}
}

// 传输层接收
ble.onMessage = function(bytes) {
	let frame = decodeShipFrame(bytes);
	
	let cmdInfo = pendingCmds[frame.fsn];
	if(cmdInfo) {
		cmdInfo.timer.clear();
		cmdInfo.promise.resolve(frame);
		delete pendingCmds[frame.fsn];
	}
}

// 同步发送数据帧
ble.sendShipFrame = async function(frame, timeout, resendTimes) {
	let bytes = encodeShipFrame(frame);
	ble.send(bytes);

	if(!timeout) { retun; }
	
	// 表示要等待命令的响应	
	let cmdInfo = {
		resendTimes: resendTimes;
		promise: new Promise();
		timer: nil;
	}
	
	cmdInfo.timer = setTimeout(function() {
		if(cmdInfo.resendTimes > 0) {
			cmdInfo.resendTimes--;
			cmdInfo.timer.reset();
			ble.send(bytes);
		} else {
			mdInfo.promise.reject();			
		}
	}, timeout);
	
	pendingCmds[frame.fsn] = cmdInfo;	
	
	retrun await cmdInfo.promise;
}
